package edu.predatorwars.client.visualization;

import java.awt.Point;

import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.collision.BoundingBox;


public class VisModel {
	private Mesh model;
	private BoundingBox box;
	private Matrix4 mvMatrix;
	private String moverText;
	
	public VisModel() {
		mvMatrix = new Matrix4();
		moverText = new String();
	}
	
	public VisModel(Point p, int len1D, int len2D, boolean randomZRotation) {
		
		FPoint fp = MathExt.calcPositionFromArrayLoc(p.x, p.y, len1D, len2D);
		mvMatrix = new Matrix4();
		moverText = new String();
		
		
		if (randomZRotation) {
			mvMatrix.setToRotation(0,0, 1, (float)Math.random() * 360f);
		}
		
		mvMatrix.val[Matrix4.M03] = fp.getX();
		mvMatrix.val[Matrix4.M13] = fp.getY();
	}
	
	public VisModel(Mesh Model, Matrix4 MVMatrix) {
		model = Model;
		mvMatrix = MVMatrix;
		moverText = new String();
		
		box = model.calculateBoundingBox().mul(mvMatrix);
	}
	
	public Mesh getModel() {
		return model;
	}

	public BoundingBox getBox() {
		return box;
	}
	
	public Matrix4 getMat4() {
		return mvMatrix;
	}
	
	public void setModel(Mesh Model) {
		model = Model;
		box = model.calculateBoundingBox().mul(mvMatrix);
	}
	
	public void move(int X, int Y, int len1D, int len2D) {
		FPoint fp = MathExt.calcPositionFromArrayLoc(X, Y, len1D, len2D);

		//calc rotation from current to new
		float px = mvMatrix.val[Matrix4.M03];
		float py = mvMatrix.val[Matrix4.M13];
		float x= fp.getX()-px;
		float y= fp.getY()-py;
		
		mvMatrix = mvMatrix.setToRotation(0,0,1, (float)Math.toDegrees(Math.atan2(x,-y)));
		
		mvMatrix.val[Matrix4.M03] = fp.getX();
		mvMatrix.val[Matrix4.M13] = fp.getY();

		box.min.x += x;
		box.min.y += y;
		box.max.x += x;
		box.max.y += y;
		

	}
	
	public void setMat4(Matrix4 MVMatrix) {
		mvMatrix = MVMatrix;
		box = box.mul(mvMatrix);
	}

	public String getMoverText() {
		return moverText;
	}
	
	public void setMoverText(String MoverText) {
		moverText = MoverText;
	}
	
}